package ynu.sxp.demoapp.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ynu.sxp.demoapp.entity.Cart;
import ynu.sxp.demoapp.service.CartService;
import ynu.sxp.demoapp.common.Result;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/CartController")
public class CartController {

    @Autowired
    private CartService cartService;

    // 获取用户的购物车信息
    @GetMapping("/listCart")
    public Result<List<Cart>> listCart(@RequestParam String userId, @RequestParam(required = false) Integer businessId) {
        List<Cart> carts;
        if (businessId != null) {
            carts = cartService.getCartByUserIdAndBusinessId(userId, businessId);
        } else {
            carts = cartService.getCartByUserId(userId);
        }
        return Result.success(carts);
    }

    // 添加商品到购物车
    @PostMapping("/saveCart")
    public Result<Void> saveCart(@RequestBody Cart cart) {
        cartService.addToCart(cart);
        return Result.success(null);
    }

    // 更新购物车中的商品数量
    @PostMapping("/updateCart")
    public Result<Void> updateCart(@RequestBody Cart cart) {
        cartService.updateCart(cart);
        return Result.success(null);
    }

    // 从购物车中移除商品
    @DeleteMapping("/removeCart")
    public Result<Void> removeCart(@RequestParam UUID cartId) {
        cartService.removeFromCart(cartId);
        return Result.success(null);
    }
}